home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / uhren & terminkalender / organizer / born21 / born.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  5KB  |  176 lines

  1. /* Born V2.1 © 1995 Echtelion. Coded by The Last Viking.  
  2.  
  3.  
  4.    This is a program that will remind you of various events. A typical
  5.    use would be to insert it in the startup-sequence and put e.g. birthdays
  6.    and other special days into the datafile.
  7.    
  8.    The package contains the following files.
  9.     
  10.    Born     (The compiled Amiga code)
  11.    Born.dta (Sample reminder file)
  12.    Born.doc (Documentary)
  13.    Born.c   (The source-code. Should be ANSI-C Compatible)         
  14.    
  15.    You can reach the author of this program on internet at:
  16.    paalde@stud.cs.uit.no                                                 */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <strings.h>
  21. #include <time.h>
  22.  
  23. FILE *fp;
  24. char ch,cha,chb;
  25. char *fname;
  26. int month,day=0;
  27.  
  28. int drd,mrd,ard; /* dayread/monthread/advance warning */
  29. int dates[10][2];
  30. static int mtab[]={31,28,31,30,31,30,31,31,30,31,30,31};
  31.  
  32. long li;
  33. struct tm *tp; 
  34.  
  35. void MError(int x);
  36. void calcnext10days(int d,int m);
  37. int leapy(int cy);
  38. int notify(int d,int m,int a);
  39. void header(void);
  40.  
  41. void main(int argc, char *argv[]){
  42.  
  43.  time(&li); tp = localtime(&li);
  44.  month=tp->tm_mon; day=tp->tm_mday;
  45.  
  46.  if (argc<2) {
  47.   header();
  48.   printf("Error 0: Parameter error, the following parameters are valid:\n\n");
  49.   printf(" %s %s",argv[0],"filename silent\n");
  50.   printf(" %s %s",argv[0],"filename\n");
  51.   printf(" %s %s",argv[0],"help\n\n");
  52.   printf("All the parameters must be written in lower-case.\n");
  53.   exit (0); 
  54.  }
  55.  fname=argv[1];
  56.  if (!((strcmp(argv[1],"help")) && (strcmp(argv[1],"?")))) {
  57.   printf("Welcome to Born 2.0.\n\n");
  58.   printf("After 3 years of pure lazyness I managed to transfer the Modula-2 code into\n");
  59.   printf("something more C'ish. I also came up with some new features for this program.\n"); 
  60.   printf("Born is not a pure birthday reminder anymore, it's now usable for all sorts\n");
  61.   printf("of reminding. You can also specify how many days in advance you want to be\n");
  62.   printf("reminded of the particular event.\n\n");
  63.   printf("The general syntax is:\n\n");
  64.   printf(" %s %s",argv[0],"filename silent\n");
  65.   printf(" %s %s",argv[0],"filename\n");
  66.   printf(" %s %s",argv[0],"help\n\n");
  67.   printf("All the parameters must be written in lower-case.\n\n");
  68.   printf("If you have any serious problems with Born, then you might be able to reach\n");
  69.   printf("me on Internet at: paalde@stud.cs.uit.no\n");
  70.   exit (0); 
  71.  }
  72.  
  73.  if (((argc>=2) && (strcmp(argv[2],"silent")))) {header(); printf("\n");}
  74.  
  75.  if (leapy(tp->tm_year+1900)) {mtab[1]++;} /* shooting year */
  76.  calcnext10days(day,month);
  77.   
  78.  if ((fp=fopen(fname,"r")) == NULL) MError(1); /* could not open file   */ 
  79.  
  80.  /* 35 = # and 10 = lf's */
  81.  while (ch=fgetc(fp)==35) {while (ch=fgetc(fp)!=10) {} }
  82.  
  83.  do {
  84.   cha=fgetc(fp); chb=fgetc(fp); /* fetch day */
  85.   if (cha!=88) {
  86.    drd=10*(cha-48)+(chb-48);
  87.    if ((cha<48) || (cha>58) || (chb<48) || (chb>58)) MError(6); /* range 0..9 */
  88.   
  89.    if (cha=fgetc(fp)!=45) MError(4);
  90.  
  91.    cha=fgetc(fp); chb=fgetc(fp); /* fetch month */
  92.    if ((cha<48) || (cha>58) || (chb<48) || (chb>58)) MError(5);
  93.    mrd=10*(cha-48)+(chb-48);
  94.  
  95.    if (cha=fgetc(fp)!=32) MError(7);
  96.  
  97.    cha=fgetc(fp);
  98.    if ((cha<48) || (cha>58)) MError(8);
  99.    ard=(cha-48);
  100.    if (cha=fgetc(fp)!=32) MError(7);
  101.  
  102.    if (!(notify(drd,mrd,ard))) {  /* was the date to be printed out? */
  103.     if (drd<10) printf("0");
  104.     printf("%d.",drd); 
  105.     if (mrd<10) printf("0");
  106.     printf("%d.%d ",mrd,tp->tm_year);    
  107.     do {
  108.      ch=getc(fp); printf("%c",ch);
  109.     } while (ch!=10);
  110.    }
  111.    else { /* no! */
  112.     do {
  113.      ch=getc(fp);
  114.     } while (ch!=10);
  115.    } /* end printout */
  116.   } /* if cha!=88 */
  117.  } while (cha!=88); /* 88 = X */
  118.  
  119.  if (fclose(fp)) MError(2);
  120. }
  121.  
  122. void header(void) {
  123.  printf("Born V2.1 © 1992-95 Ecthelion. Time ");
  124.  if (tp->tm_hour<10) printf("0"); /* write time information */
  125.  printf("%d%s",tp->tm_hour,":");
  126.  if (tp->tm_min<10) printf("0");
  127.  printf("%d",tp->tm_min);
  128.  printf("  Date "); /* write date information */
  129.  if (day<10) printf("0");
  130.  printf("%d.",day); 
  131.  if (month+1<10) printf("0");
  132.  printf("%d.",month+1);
  133.  printf("%d\n",tp->tm_year);
  134. }
  135.  
  136. int notify(int d,int m,int a){
  137.  int cnt=0;
  138.  int found=1;
  139.  do {
  140.   if ((dates[cnt][0]==d) && (dates[cnt][1]==m)) {found=0;}
  141.   cnt++;
  142.  } while (cnt<a);
  143.  return found;
  144. }
  145.  
  146. void calcnext10days(int d,int m){
  147.  int cnt=0;
  148.  while (cnt<=9) {
  149.   dates[cnt][0]=d; dates[cnt][1]=m+1;
  150.   cnt++;
  151.   d++;
  152.   if (d>mtab[m]) {
  153.    m++;d=1;
  154.    if (m>11) m=0;
  155.   }
  156.  }
  157. }
  158.  
  159. int leapy(int cy){
  160.  return ((int) ((cy % 4 == 0) && ((cy % 100 >0) || (cy % 400 == 0))));
  161. }
  162.  
  163. void MError(int x){   
  164.  if (x==1) printf("%s%s%s","Error 1: Can not open data-file: ",fname,".");
  165.  if (x==2) printf("file close error.");
  166.  if (x==3) printf("file read error.");
  167.  if (x==4) printf("Error 4: Syntax error in datafile, missing - .");
  168.  if (x==5) printf("Error 5: Syntax error in datafile, missing MONTH.");
  169.  if (x==6) printf("Error 6: Syntax error in datafile, missing DAY.");
  170.  if (x==7) printf("Error 7: Syntax error in datefile, missing ' '.");
  171.  if (x==8) printf("Error 8: Syntax error in datefile, missing ADVANCE_DAYS.");
  172.  printf("\n\n");
  173.  exit(0);
  174. }
  175.  
  176.